package in.muton.edushare.poll.questions;

import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.poll.pages.PollEditSingleQ;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.PollsDAO;
import com.edushare.common.vo.Poll;
import com.edushare.common.vo.PollQuestions;

public abstract class QuestionBase extends EdusharePanel {

	private IModel<PollQuestions> questionModel;
	public boolean editMode;
	public boolean preview;

	public QuestionBase(String id, IModel<PollQuestions> model,
			EduShareBasePage page, final boolean preview, final boolean editMode) {
		super(id, page);
		this.setQuestionModel(model);
		this.editMode = editMode;
		add(new MultiLineLabel("question", questionModel.getObject()
				.getQuestionSerialNo()
				+ ". " + questionModel.getObject().getQuestion()));

		add(new DeleteLink("deleteLink"));
		add(new EditLink("editLink"));
	}

	public abstract boolean verify();

	public abstract boolean save(boolean draft);

	public abstract boolean retrieveValues();

	public abstract void clear();

	private class EditLink extends Link<Void> {
		private static final long serialVersionUID = 3397411678540860990L;

		public EditLink(String id) {
			super(id);
		}

		@Override
		public void onClick() {
			PageParameters params = new PageParameters();
			params.put("0", questionModel.getObject().getPoll().getPollId()
					.toString());
			params.put("1", questionModel.getObject().getQuestionSerialNo()
					.toString());
			setResponsePage(PollEditSingleQ.class, params);
		}

		@Override
		public boolean isVisible() {
			return editMode
					&& (questionModel.getObject().getPoll().getStatus() == Poll.POLL_STATUS_DRAFT);
		}
	}

	private class DeleteLink extends Link<Void> {
		private static final long serialVersionUID = 3884051248422953269L;

		public DeleteLink(String id) {
			super(id);
		}

		@Override
		public void onClick() {
			new PollsDAO(getORMInterface()).deleteQuestion(getQuestionModel()
					.getObject());
		}

		@Override
		public boolean isVisible() {
			return editMode
					&& (questionModel.getObject().getPoll().getStatus() == Poll.POLL_STATUS_DRAFT);
		}
	}

	public void setQuestionModel(IModel<PollQuestions> questionModel) {
		this.questionModel = questionModel;
	}

	public IModel<PollQuestions> getQuestionModel() {
		return questionModel;
	}

	public boolean isEditMode() {
		return editMode;
	}

	public void setEditMode(boolean editMode) {
		this.editMode = editMode;
	}

	public boolean isPreview() {
		return preview;
	}

	public void setPreview(boolean preview) {
		this.preview = preview;
	}

	@Override
	public boolean isTransparentResolver() {
		return true;
	}

	private static final long serialVersionUID = -7361600632643591916L;
}
